import time

from pymongo import MongoClient
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from concurrent.futures import ThreadPoolExecutor
from Lactationer_Slave_Selenium.redisClient import redisClient

class Execuer_Elephant:

    def __init__(self):
        # 代理设置
        co = Options()
        prefs = {"profile.managed_default_content_settings.images":2}
        co.add_experimental_option("prefs",prefs)
        co.add_argument('lang=zh_CN.UTF-8')
        co.add_argument('--headless')
        co.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
        self.options = co

    def saveIteem(self, item):
        print(item)
        client = MongoClient(host='127.0.0.1',
                             port=27017)
        # 连接表
        mydb = client["phonics"]
        mycol = mydb["Lactationer"]
        mycol.insert_one(dict(item))

    def executeFiledRequest(self, url):
        self.goRequest(url)

    def goRequest(self, url):
        r = redisClient()
        proxy_ip = r.getEffectiveIp()
        try:
            co = self.options
            co.add_argument("--proxy-server=" + proxy_ip)
            driver = webdriver.Chrome(chrome_options=co, executable_path='/Users/liyang/Applications/chromedriver')
            # 设置超时时间为10S，超出10s重新更换代理请求
            driver.set_page_load_timeout(10)
            driver.get(url)
            button = driver.find_element_by_class_name('displayphonenumber')
            button.click()
            time.sleep(1)
            # phone
            phone = driver.find_element_by_class_name("show_noauth_pop")
            # title
            title = driver.find_element_by_xpath("//*[@id='wrapper']/div[4]/div[1]/div[2]/h1")
            # provide
            provide = driver.find_element_by_xpath("//*[@id='wrapper']/div[4]/div[2]/ul/li[1]/div")
            # address
            address = driver.find_element_by_xpath("//*[@id='wrapper']/div[4]/div[2]/ul/li[2]/div/p")
            item = {
                "phone": phone.text,
                "title": title.text,
                "provide": provide.text,
                "address": address.text,
            }
            self.saveIteem(item)
        except:
            print("请求失败，删除无效ip:%s" % proxy_ip)
            r.rmrIp(proxy_ip)
            self.executeFiledRequest(url)

    def execute(self):
        r = redisClient()
        while True:
            detail_url = r.getEffectiveUrl
            if detail_url is not None:
                self.goRequest(detail_url)
            else:
                break
        driver = webdriver.Chrome(executable_path='/Users/liyang/Applications/chromedriver')
        driver.close()




# 查看本机ip，查看代理是否起作用
# driver.get("http://httpbin.org/ip")
# print(driver.page_source)

e = Execuer_Elephant()
e.execute()
